package com.outsider.util;



import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;

import com.outsider.movie.dao.Movie;
import com.outsider.util.pageprocessor.DyttPageprocessor;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.ConsolePipeline;
import us.codecraft.webmagic.pipeline.Pipeline;
import us.codecraft.webmagic.processor.PageProcessor;

public class MovieUtil {
	private static final String START_URL="http://www.dytt8.net/html/gndy/dyzz/index.html";
	
	static class DyttPipeline implements Pipeline{
		public void process(ResultItems resultItems, Task task) {
			if (null!=resultItems.get("title")){
	            Movie movie=new Movie();
	            movie.setTitle(resultItems.get("title").toString());
	            movie.setUrl(resultItems.get("url").toString());
	            movie.setDyttID(resultItems.get("dyttID"));
	            movie.setDate(resultItems.get("date"));
	            movie.setDoubanID(DoubanUtil.getMovieID(movie.getTitle()));
	            movie.setRating(DoubanUtil.getRating(movie.getDoubanID()));
	            movie.setTags(DoubanUtil.getTags(movie.getDoubanID()));
	            movie.save();
			}
		}
	}
    public static Runnable getMovieTask(){
    	Runnable task=new Runnable() {
			public void run() {
				Spider.create(new DyttPageprocessor())
				.addUrl("http://www.dytt8.net/html/gndy/dyzz/index.html")
				.addPipeline(new DyttPipeline()).thread(3).run();
				Logger.getLogger("").info("Movie Update Success");

			}
		};
		return task;
    }
	

}
